﻿namespace ConsoleAppGroupBy
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, World!");
            List<Device> list = new List<Device>();
            list.Add(
                new Device()
                {
                    Id = 101,
                    Name = "1号设备",
                    Type = "生产设备",
                    Result = 99
                }
            );
            list.Add(
                new Device()
                {
                    Id = 102,
                    Name = "2号设备",
                    Type = "生产设备",
                    Result = 60
                }
            );
            list.Add(
                new Device()
                {
                    Id = 103,
                    Name = "3号设备",
                    Type = "测试设备",
                    Result = 98
                }
            );
            list.Add(
                new Device()
                {
                    Id = 104,
                    Name = "4号设备",
                    Type = "测试设备",
                    Result = 70
                }
            );
            list.Add(
                new Device()
                {
                    Id = 201,
                    Name = "5号生产设备",
                    Type = "生产设备",
                    Result = 100
                }
            );
            list.Add(
                new Device()
                {
                    Id = 202,
                    Name = "6号测试设备",
                    Type = "测试设备",
                    Result = 89
                }
            );
            list.Add(
                new Device()
                {
                    Id = 203,
                    Name = "7号测试设备",
                    Type = "测试设备",
                    Result = 98
                }
            );
            list.Add(
                new Device()
                {
                    Id = 204,
                    Name = "8号测试设备",
                    Type = "测试设备",
                    Result = 95
                }
            );

            var groups = list.GroupBy(x => x.Type);
            foreach (var group in groups)
            {
                Console.WriteLine(group.Key);
                foreach (var item in group)
                {
                    Console.WriteLine(
                        $"ID:{item.Id},Name:{item.Name},Type:{item.Type},Result:{item.Result}"
                    );
                }
            }

            Console.WriteLine();

            var groups2 = list.GroupBy(x => x.Result > 90);
            foreach (var group in groups2)
            {
                Console.WriteLine(group.Key);
                foreach (var item in group)
                {
                    Console.WriteLine(
                        $"ID:{item.Id},Name:{item.Name},Type:{item.Type},Result:{item.Result}"
                    );
                }
            }
            Console.WriteLine();
            Console.WriteLine();

            var group3 = list.GroupBy(
                item => item.Result / 10,
                pet => pet.Result,
                (baseResult, results) =>
                    new
                    {
                        Key = baseResult,
                        Count = results.Count(),
                        Min = results.Min(),
                        Max = results.Max(),
                    }
            );

            foreach (var group in group3)
            {
                Console.WriteLine($"Group:{group.Key * 10}--{group.Key * 10 + 9}");
                Console.WriteLine($"Count:{group.Count}");
                Console.WriteLine($"Min:{group.Min}");
                Console.WriteLine($"Max:{group.Max}");
                Console.WriteLine();
            }
        }
    }

    /// <summary>
    /// 设备类
    /// </summary>
    class Device
    {
        /// <summary>
        /// Id
        /// </summary>
        public int Id { get; set; }

        /// <summary>
        /// 设备类型
        /// </summary>
        public string Type { get; set; }

        /// <summary>
        /// 名称
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 测试结果
        /// </summary>
        public int Result { get; set; }
    }
}
